package com.dream.testtubebaby.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.widget.Toast;

public class PictureUtil {

	public static String bitmapToString(String filePath) {

		Bitmap bm = getSmallBitmap(filePath);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
		byte[] b = baos.toByteArray();

		return Base64.encodeToString(b, Base64.DEFAULT);

	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

			// Calculate ratios of height and width to requested height and
			// width
			final int heightRatio = Math.round((float) height
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) width / (float) reqWidth);

			// Choose the smallest ratio as inSampleSize value, this will
			// guarantee
			// a final image with both dimensions larger than or equal to the
			// requested height and width.
			inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}

		return inSampleSize;
	}

	public static Bitmap getSmallBitmap(String filePath) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(filePath, options);

		// Calculate inSampleSize
		//options.inSampleSize = calculateInSampleSize(options, 480, 800);
		options.inSampleSize = calculateInSampleSize(options, 3072, 4096);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;

		return BitmapFactory.decodeFile(filePath, options);
	}

	public static void deleteTempFile(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	public static void galleryAddPic(Context context, String path) {
		Intent mediaScanIntent = new Intent(
				Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		File f = new File(path);
		Uri contentUri = Uri.fromFile(f);
		mediaScanIntent.setData(contentUri);
		context.sendBroadcast(mediaScanIntent);
	}

	public static File getAlbumDir() {
		File dir = new File(Environment.getExternalStorageDirectory(),
				getAlbumName());
		if (!dir.exists()) {
			dir.mkdirs();
		}
		return dir;
	}

	public static String getAlbumName() {
		return "testtubebaby";
	}

	public static String CompressPicture(Context mContext,
			String mCurrentPhotoPath, boolean isDelete) {
		if (mCurrentPhotoPath != null) {
			try {
				File f = new File(mCurrentPhotoPath);
				Bitmap bm = PictureUtil.getSmallBitmap(mCurrentPhotoPath);
				File objectFile = new File(PictureUtil.getAlbumDir(), "small_"
						+ f.getName());
				FileOutputStream fos = new FileOutputStream(objectFile);
				bm.compress(Bitmap.CompressFormat.JPEG, 20, fos);
				if(isDelete){
					f.delete();
				}
				return objectFile.getAbsolutePath();
			} catch (Exception e) {
				Toast.makeText(mContext, "error!", Toast.LENGTH_SHORT).show();
				return null;
			}

		} else {
			Toast.makeText(mContext, "file not exsit!", Toast.LENGTH_SHORT)
					.show();
			return null;
		}
	}
}
